package com.adui.mgt.performance.api;

import com.adui.mgt.performance.common.Result;
import com.adui.mgt.performance.entity.Department;
import com.adui.mgt.performance.entity.User;
import com.adui.mgt.performance.service.DepartmentService;
import com.adui.mgt.performance.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 部门crud+人员设置
 **/
@RestController
@RequestMapping("/api/departments")
public class DepartmentApi {

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") String id) {
        return Result.data(departmentService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestBody Department department) {
        return departmentService.findPage(department);
    }

    @PostMapping("/")
    public Result add(@RequestBody Department department) {
        departmentService.getBaseMapper().insert(department);
        return Result.ok();
    }

    @PutMapping("/")
    public Result update(@RequestBody Department department) {
        departmentService.getBaseMapper().updateById(department);
        return Result.ok();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id) {
        departmentService.removeById(id);
        return Result.ok();
    }

    /**
     * 给人加部门
     * @param id
     * @param userIds
     * @return
     */
    @PostMapping("/users/{id}")
    public Result addMembers2Department(@PathVariable("id") String id, @RequestBody List<String> userIds) {
        List<User> users = userService.listByIds(userIds);
        users.forEach(user -> user.setDepId(id));
        userService.updateBatchById(users);
        return Result.ok();
    }

}
